﻿namespace ModulePlaces.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ModulesInfrastructure.ViewModels;
    using Microsoft.Practices.Prism.Events;
    using ServerConnection.ShopServiceReference;
    using ServerConnection;
    using EventInfrastructure;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;
    using System.ComponentModel;

    using System.Windows;
    using System.Windows.Input;

    using Microsoft.Practices.Unity;
    using ShellInfrastructure;
    using ModulePlaces.Views;
    using System.Collections;
    using System.Collections.ObjectModel;

    public  class PlacesViewModel : SharedViewModel, IPlacesViewModel, INotifyPropertyChanged
    {
        #region Private members

        private IEventAggregator m_eventAggregator;

        private List<PlaceView> m_placeViewList = new List<PlaceView>();

        private ObservableCollection<PlaceView> placesCollection;// = new ObservableCollection<PlaceView>();

        private PlaceView m_selectedPlace;

        [Dependency]
        public IRegionManager RegionManager { get; set; }      
                 

        private void m_onUpdatePlacesList(int id)
        {
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IPlacesViewList>());

            //this.ShowProduct = new List<ProductFull>();
            //this.ShowProduct.Add(Service.Instance.GetProductFullById(id, "eng"));
            //RaisePropertyChanged(() => this.ShowProduct);

            //LoadPlaces = Service.Instance.GetPlaces("eng").ToList();

            List<PlaceView> tmpPlaceView = Service.Instance.GetPlaces("eng").ToList();
            placesCollection = new ObservableCollection<PlaceView>(tmpPlaceView);

            LoadPlaces = placesCollection;
            LoadPlaces = placesCollection;
            LoadPlaces = placesCollection;         
        }

        /// <summary>
        /// This method is called up when the property is changed
        /// </summary>
        /// <param name="propertyName"></param>
        private void m_OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                //fire event
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion Private members


        #region Constructor

        public PlacesViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator = eventAggregator;
            //LoadPlaces = Service.Instance.GetPlaces("eng").ToList()ж as ObservableCollection<PlaceView>;
            List<PlaceView> tmpPlaceView = Service.Instance.GetPlaces("eng").ToList();
            placesCollection = new ObservableCollection<PlaceView>(tmpPlaceView);
            m_eventAggregator.GetEvent<AddOrUpdatePlaceEvent>().Subscribe(m_onUpdatePlacesList);
        }

        #endregion Constructor

        //public IEnumerable<PlaceView> LoadPlaces
        //{
        //    get
        //    {                
        //        foreach (PlaceView elem in m_placeViewList)
        //            yield return elem;  
        //    }
        //    set
        //    {
        //        m_placeViewList = (List<PlaceView>)value;
        //        m_OnPropertyChanged("LoadPlaces");
        //    }
        //}

        #region Public properties

        public ObservableCollection<PlaceView> LoadPlaces
        {
            get
            {
                return placesCollection;
            }
            set
            {
                placesCollection = value;
                m_OnPropertyChanged("LoadPlaces");
            }
        }

        #endregion Public properties

        public PlaceView OnSelectedPlace
        {
            get
            {
                return m_selectedPlace;
            }
            set
            {
                m_selectedPlace = value;

                if (m_selectedPlace != null)
                {
                    m_eventAggregator.GetEvent<PlaceSelectEvent>().Publish(m_selectedPlace.Name);
                    //RaisePropertyChanged(() => this.LoadSubCategory);
                }
            }
        }



        #region Events

        /// <summary>
        /// PropertyChanged is needed for binding to ui
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

    }//class
}
